package com.yanqu.road.server.manager.activity.sonhai.pb;

import com.yanqu.road.entity.activity.sonhai.data.*;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiRewardFlagEnum;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiWinFlagEnum;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiBattleOneResp;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiCreatePlotResp;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiNewBattleResp;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiPlotBuffResp;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SonHaiActivityPb {

    /**
     * build 玩家
     * @param user
     * @param syncRedDot 废弃
     * @return
     */
    public static SonHaiProto.SonHaiUserData.Builder buildSonHaiUserData(CrossSonHaiUser user, CrossSonHaiTeam team, boolean syncRedDot) {
        SonHaiProto.SonHaiUserData.Builder builder = SonHaiProto.SonHaiUserData.newBuilder();
        builder.setUserId(user.getUserId());
        builder.setTeamId(user.getTeamId());
        builder.setServerId(user.getServerId());
        builder.setEnergy(user.getEnergy());
        builder.setBloodRate(user.getBloodRate());
        builder.setScore(user.getScore());
        builder.setAuraScore(user.getAuraScore());
        builder.setRank(user.getUserRank());
        builder.setDieFlag(user.getDieFlag());
        for (SonHaiWaitSelectBuffData waitSelectBuffData : user.getWaitSelectBuffList()) {
            builder.addWaitSelectBuff(buildSonHaiWaitSelectBuffData(waitSelectBuffData));
        }
        for (SonHaiBuffData buff : user.getBuffMap().values()) {
            builder.addBuffs(buildSonHaiBuff(buff));
        }
        builder.setRegionId(user.getRegionId());
        builder.setX(user.getX());
        builder.setY(user.getY());
        builder.setScoreAddRate(user.getScoreAddRate(user.getRegionData()));
        builder.setProgressAddRate(user.getProgressOpRateNotCondition(user.getRegionData()));
        //区域数据
        if (user.getRegionData() != null) {
            SonHaiProto.SonHaiRegionData.Builder regionDataBuilder = buildSonHaiRegionData(user.getRegionData(), team);
            builder.setRegionData(regionDataBuilder);
        }
        //占领的兽蛋
        for (SonHaiEggData eggData : user.getUserEggDataMap().values()) {
            builder.addEggs(buildSonHaiEggData(eggData, user.getLanguage()));
        }
        //死亡相关信息
        if (user.getDieData() != null) {
            builder.setDieData(buildSonHaiUserDieData(user.getDieData()));
        }
        builder.setEggRewardNum(user.getNotReceiveEggRewardNum());
        builder.setEnergyLastTime(user.getEnergyRestoreTime());
        builder.setNextBuffComeNeedEnergy(user.getNextBuffComeNeedEnergy());
        builder.setAllBlood(user.getAllBlood());
        builder.setReBlood(user.getReBlood());
        builder.setBasePower(user.getPower());
        builder.setPvePower(user.getPowerForPve());
        builder.setPvpPower(user.getPowerForPvp());
        builder.setPvePowerAddRate(user.getPowerAddRateForPve(user.getRegionData()));
        builder.setPvpPowerAddRate(user.getPowerAddRateForPvp());
        builder.setTalentPowerAddRate(user.getTalentAddition());
        builder.setHelpNum(user.getHelpNum());
//        if (syncRedDot) {
//            builder.setRedDot(buildSonHaiRedDotData(user));
//        }
        return builder;
    }

    /**
     * build 玩家-简易版
     * @param user
     * @param syncRedDot 废弃
     * @return
     */
    public static SonHaiProto.SonHaiUserDataSimple.Builder buildSonHaiUserDataSimple(CrossSonHaiUser user, boolean syncRedDot) {
        SonHaiProto.SonHaiUserDataSimple.Builder builder = SonHaiProto.SonHaiUserDataSimple.newBuilder();
        builder.setUserId(user.getUserId());
        builder.setEnergy(user.getEnergy());
        builder.setBloodRate(user.getBloodRate());
        builder.setScore(user.getScore());
        builder.setAuraScore(user.getAuraScore());
        for (SonHaiWaitSelectBuffData waitSelectBuffData : user.getWaitSelectBuffList()) {
            builder.addWaitSelectBuff(buildSonHaiWaitSelectBuffData(waitSelectBuffData));
        }
        for (SonHaiBuffData buff : user.getBuffMap().values()) {
            builder.addBuffs(buildSonHaiBuff(buff));
        }
        builder.setScoreAddRate(user.getScoreAddRate(user.getRegionData()));
        builder.setProgressAddRate(user.getProgressOpRateNotCondition(user.getRegionData()));
        builder.setProgress(user.getProgress());
        builder.setRank(user.getUserRank());
        builder.setDieFlag(user.getDieFlag());
        for (SonHaiEggData eggData : user.getUserEggDataMap().values()) {
            builder.addEggs(buildSonHaiEggData(eggData, user.getLanguage()));
        }
        //死亡相关信息
        if (user.getDieData() != null) {
            builder.setDieData(buildSonHaiUserDieData(user.getDieData()));
        }
        builder.setEggRewardNum(user.getNotReceiveEggRewardNum());
        builder.setEnergyLastTime(user.getEnergyRestoreTime());
        builder.setNextBuffComeNeedEnergy(user.getNextBuffComeNeedEnergy());
        builder.setAllBlood(user.getAllBlood());
        builder.setReBlood(user.getReBlood());
        builder.setBasePower(user.getPower());
        builder.setPvePower(user.getPowerForPve());
        builder.setPvpPower(user.getPowerForPvp());
//        boolean rewardFlag = false;
//        if (user.getRegionData() != null) {
//            for (SonHaiPlotData plotData : user.getRegionData().getPlotDataMap().values()) {
//                if (plotData.getWinFlag() == SonHaiWinFlagEnum.WIN_YES.getType() && plotData.getGetRewardFlag() == SonHaiRewardFlagEnum.GET_NOT.getType()) {
//                    rewardFlag = true;
//                    break;
//                }
//            }
//            builder.setBattleBossFlag(user.checkCanBattleBoss());
//        }
//        builder.setRewardFlag(rewardFlag);
        builder.setPvePowerAddRate(user.getPowerAddRateForPve(user.getRegionData()));
        builder.setPvpPowerAddRate(user.getPowerAddRateForPvp());
        builder.setTalentPowerAddRate(user.getTalentAddition());
        builder.setHelpNum(user.getHelpNum());
//        if (syncRedDot) {
//            builder.setRedDot(buildSonHaiRedDotData(user));
//        }
        return builder;
    }

    /**
     * 红点数据
     * @param user
     * @return
     */
    public static SonHaiProto.SonHaiRedDotData.Builder buildSonHaiRedDotData(CrossSonHaiUser user) {
        SonHaiProto.SonHaiRedDotData.Builder builder = SonHaiProto.SonHaiRedDotData.newBuilder();
        builder.setBossRedDot(user.getBossRedDot());
        builder.setEggRewardRedDot(user.getEggRewardRedDot());
        builder.setUserHelpRedDot(user.getUserHelpRedDot());
        return builder;
    }

    /**
     * 构建待选择的BUFF数据
     * @param data
     * @return
     */
    public static SonHaiProto.SonHaiWaitSelectBuffData.Builder buildSonHaiWaitSelectBuffData(SonHaiWaitSelectBuffData data) {
        SonHaiProto.SonHaiWaitSelectBuffData.Builder builder = SonHaiProto.SonHaiWaitSelectBuffData.newBuilder();
        builder.setWaitSelectBuff(StringUtils.setToString(data.getWaitSelectBuffSet(), ","));
        builder.setResetRandomNum(data.getResetRandomNum());
        return builder;
    }

    /**
     * build buff
     * @param buff
     * @return
     */
    public static SonHaiProto.SonHaiBuff.Builder buildSonHaiBuff(SonHaiBuffData buff) {
        SonHaiProto.SonHaiBuff.Builder builder = SonHaiProto.SonHaiBuff.newBuilder();
        builder.setBuffId(buff.getBuffId());
        builder.setLevel(buff.getLevel());
        return builder;
    }

    /**
     * build 区域数据
     * @param regionData
     * @param team
     * @return
     */
    public static SonHaiProto.SonHaiRegionData.Builder buildSonHaiRegionData(SonHaiRegionData regionData, CrossSonHaiTeam team) {
        SonHaiProto.SonHaiRegionData.Builder builder = SonHaiProto.SonHaiRegionData.newBuilder();
        builder.setRegionId(regionData.getRegionId());
        builder.setProgress(regionData.getProgress());
        //区域地块
        for (SonHaiPlotData plotData : regionData.getPlotDataMap().values()) {
            SonHaiProto.SonHaiPlotData.Builder plotBuilder = buildSonHaiSimplePlotData(plotData);
            builder.addPlots(plotBuilder);
        }
        //成员玩家位置
        for (CrossSonHaiUser member : team.getMemberMap().values()) {
            if (regionData.getRegionId() == member.getRegionId()) {
                SonHaiProto.SonHaiUserPosition.Builder memberPosition = buildSonHaiUserPosition(member);
                builder.addUserPositions(memberPosition);
            }
        }
        //兽王数据
        if (regionData.getBeastBoss() != null) {
            SonHaiProto.SonHaiBeastBossData.Builder beastBoss = builderSonHaiBeastBossData(regionData.getBeastBoss());
            builder.setBeastBoss(beastBoss);
        }
        builder.setMapId(regionData.getMapId());
        //重置区域投票单
        if (regionData.getLaunchRestUserId() > 0) {
            //区域里的玩家
            Set<Long> userIds = team.getRegionUserIds(regionData.getRegionId());
            SonHaiProto.SonHaiRegionRestOrderData.Builder restOderBuilder = SonHaiProto.SonHaiRegionRestOrderData.newBuilder();
            restOderBuilder.setRegionId(regionData.getRegionId());
            restOderBuilder.setLaunchUserId(regionData.getLaunchRestUserId());
            restOderBuilder.addAllUserIds(userIds);
            restOderBuilder.addAllAgreeUserIds(regionData.getAgreeRestUserIds());
            restOderBuilder.setAutoRestTime(regionData.getAutoRestTime());
            builder.setRestOrder(restOderBuilder);
        }
        return builder;
    }

    /**
     * build 异兽数据
     * @param beastData
     * @return
     */
    public static SonHaiProto.SonHaiBeastData.Builder buildSonHaiBeastData(SonHaiBeastData beastData) {
        SonHaiProto.SonHaiBeastData.Builder builder = SonHaiProto.SonHaiBeastData.newBuilder();
        builder.setId(beastData.getId());
        builder.setAllBlood(beastData.getAllBlood());
        builder.setReBlood(beastData.getAllReBlood());
        for (SonHaiReBloodInfo reBloodInfo : beastData.getReBloodInfoMap().values()) {
            SonHaiProto.SonHaiReBloodInfo.Builder reBloodBuilder = buildSonHaiReBloodInfo(reBloodInfo);
            builder.addReBloodInfos(reBloodBuilder);
        }
        return builder;
    }

    /**
     * build 掉血详情
     * @param reBloodInfo
     * @return
     */
    public static SonHaiProto.SonHaiReBloodInfo.Builder buildSonHaiReBloodInfo(SonHaiReBloodInfo reBloodInfo) {
        SonHaiProto.SonHaiReBloodInfo.Builder builder = SonHaiProto.SonHaiReBloodInfo.newBuilder();
        builder.setUserId(reBloodInfo.getUserId());
        builder.setReBlood(reBloodInfo.getReBlood());
        return builder;
    }

    /**
     * build 兽王数据
     * @param beastBossData
     * @return
     */
    public static SonHaiProto.SonHaiBeastBossData.Builder builderSonHaiBeastBossData(SonHaiBeastBossData beastBossData) {
        SonHaiProto.SonHaiBeastBossData.Builder builder = SonHaiProto.SonHaiBeastBossData.newBuilder();
        SonHaiProto.SonHaiBeastData.Builder beastBoss = buildSonHaiBeastData(beastBossData.getBeastData());
        builder.setEventId(beastBossData.getEventId());
        builder.setRegionId(beastBossData.getRegionId());
        builder.setBeastData(beastBoss);
        builder.setWinFlag(beastBossData.getWinFlag());
        for (long rewardUserId : beastBossData.getRewardUserIdSet()) {
            builder.addGetRewardUserIds(rewardUserId);
        }
        for (Map.Entry<Long, Integer> entry : beastBossData.getAttNumMap().entrySet()) {
            SonHaiProto.SonHaiAttNumData.Builder attNumBuilder = SonHaiProto.SonHaiAttNumData.newBuilder();
            attNumBuilder.setUserId(entry.getKey());
            attNumBuilder.setAttNum(entry.getValue());
            builder.addAttNums(attNumBuilder);
        }
        builder.setX(beastBossData.getPositionList().get(0).getX());
        builder.setY(beastBossData.getPositionList().get(0).getY());
        return builder;
    }

    /**
     * build 兽群数据
     * @param beastGroupData
     * @return
     */
    public static SonHaiProto.SonHaiBeastGroupData.Builder buildSonHaiBeastGroupData(SonHaiBeastGroupData beastGroupData) {
        SonHaiProto.SonHaiBeastGroupData.Builder builder = SonHaiProto.SonHaiBeastGroupData.newBuilder();
        builder.setId(beastGroupData.getId());
        for (SonHaiBeastData beastData : beastGroupData.getBeastDataMap().values()) {
            SonHaiProto.SonHaiBeastData.Builder beastBuilder = buildSonHaiBeastData(beastData);
            builder.addBeasts(beastBuilder);
        }
        return builder;
    }

    /**
     * build 玩家位置
     * @param user
     * @return
     */
    public static SonHaiProto.SonHaiUserPosition.Builder buildSonHaiUserPosition(CrossSonHaiUser user) {
        SonHaiProto.SonHaiUserPosition.Builder builder = SonHaiProto.SonHaiUserPosition.newBuilder();
        builder.setUserId(user.getUserId());
        builder.setX(user.getX());
        builder.setY(user.getY());
        return builder;
    }

    /**
     * build 地块-简易版
     * @param plotData
     * @return
     */
    public static SonHaiProto.SonHaiPlotData.Builder buildSonHaiSimplePlotData(SonHaiPlotData plotData) {
        SonHaiProto.SonHaiPlotData.Builder builder = SonHaiProto.SonHaiPlotData.newBuilder();
        builder.setUserId(plotData.getUserId());
        builder.setRegionId(plotData.getRegionId());
        builder.setId(plotData.getPlotId());
        builder.setType(plotData.getType());
        builder.setLevel(plotData.getLevel());
        builder.setX(plotData.getX());
        builder.setY(plotData.getY());
        builder.setWinFlag(plotData.getWinFlag());
        builder.setGetRewardFlag(plotData.getGetRewardFlag());
        builder.setChooseRewardFlag(plotData.getChooseRewardFlag());
        builder.setAuraScore(plotData.getExtraAuraScore());
        for (Map.Entry<Long, Integer> entry : plotData.getAttNumMap().entrySet()) {
            SonHaiProto.SonHaiAttNumData.Builder attNumBuilder = SonHaiProto.SonHaiAttNumData.newBuilder();
            attNumBuilder.setUserId(entry.getKey());
            attNumBuilder.setAttNum(entry.getValue());
            builder.addAttNums(attNumBuilder);
        }
        if (plotData.getWinFlag() == SonHaiWinFlagEnum.WIN_NOT.getType()) {
            if (plotData.getBeastData() != null) {
                builder.setBloodRate(plotData.getBeastData().getBloodRate());
            }
            if (plotData.getPvpUserId() > 0) {
                CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(plotData.getActivityId(), plotData.getGroupId());
                if (activity != null) {
                    CrossSonHaiUser pvpUser = activity.getUser(plotData.getPvpUserId());
                    if (pvpUser != null) {
                        builder.setBloodRate(pvpUser.getBloodRate());
                    }
                }
            }
            if (plotData.getEggId() > 0) {
                CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(plotData.getActivityId(), plotData.getGroupId());
                if (activity != null) {
                    SonHaiEggData egg = activity.getEgg(plotData.getEggId());
                    if (egg != null) {
                        CrossSonHaiUser plotUser = activity.getUser(plotData.getUserId());
                        if (plotUser != null) {
                            builder.setEgg(buildSonHaiEggData(egg, plotUser.getLanguage()));
                        }
                    }
                }
            }
        }
        return builder;
    }

    /**
     * build 地块-详细版
     * @param plotData 地块基础数据
     * @param beastBossData 兽王数据
     * @param pvpUser pvp玩家数据
     * @param eggData 兽蛋数据
     * @return
     */
    public static SonHaiProto.SonHaiPlotData.Builder buildSonHaiPlotData(CrossSonHaiUser user,
                                                                         SonHaiRegionData regionData,
                                                                         SonHaiPlotData plotData,
                                                                         SonHaiBeastBossData beastBossData,
                                                                         CrossSonHaiUser pvpUser,
                                                                         SonHaiEggData eggData) {
        SonHaiProto.SonHaiPlotData.Builder builder = SonHaiProto.SonHaiPlotData.newBuilder();
        builder.setUserId(plotData.getUserId());
        builder.setRegionId(plotData.getRegionId());
        builder.setId(plotData.getPlotId());
        builder.setType(plotData.getType());
        builder.setLevel(plotData.getLevel());
        builder.setX(plotData.getX());
        builder.setY(plotData.getY());
        builder.setWinFlag(plotData.getWinFlag());
        builder.setGetRewardFlag(plotData.getGetRewardFlag());
        builder.setChooseRewardFlag(plotData.getChooseRewardFlag());
        builder.setAuraScore(plotData.getExtraAuraScore());
        for (Map.Entry<Long, Integer> entry : plotData.getAttNumMap().entrySet()) {
            SonHaiProto.SonHaiAttNumData.Builder attNumBuilder = SonHaiProto.SonHaiAttNumData.newBuilder();
            attNumBuilder.setUserId(entry.getKey());
            attNumBuilder.setAttNum(entry.getValue());
            builder.addAttNums(attNumBuilder);
        }

        //异兽战力加成
        int beatsPowerAddRate = regionData.getPlotBeatsBattlePowerAddRate(plotData.getX(), plotData.getY(), user.getConfig());
        builder.setBeastPowerAddRate(beatsPowerAddRate);
        //积分减少率
        int scoreReRate = regionData.getPlotScoreReduceRate(plotData.getX(), plotData.getY(), user.getConfig());
        builder.setScoreReRate(scoreReRate);
        //寻踪进度减少值
        int progressReValue = regionData.getPlotProgressReduceValue(plotData.getX(), plotData.getY(), user.getConfig());
        builder.setProgressReValue(progressReValue);

        //异兽数据
        if (plotData.getBeastData() != null) {
            SonHaiProto.SonHaiBeastData.Builder beastBuilder = buildSonHaiBeastData(plotData.getBeastData());
            builder.setBeastData(beastBuilder);
            builder.setBloodRate(plotData.getBeastData().getBloodRate());
            if (plotData.getUserId() != user.getUserId()) {
                builder.setHelpCdEndTime(plotData.getBeastData().getHelpCdEndTime(user.getUserId(), user.getConfig().getHelpCd()));
            }
        }
        //pvp玩家数据
        if (pvpUser != null) {
            SonHaiProto.SonHaiPvPUserData.Builder pvpUserbuilder = buildSonHaiPvPUserData(pvpUser, null);
            builder.setPvpUser(pvpUserbuilder);
            builder.setBloodRate(pvpUser.getBloodRate());
            if (plotData.getUserId() != user.getUserId()) {
                builder.setHelpCdEndTime(pvpUser.getHelpCdEndTime(user.getUserId()));
            }
        }
        //蛋数据
        if (eggData != null) {
            SonHaiProto.SonHaiEggData.Builder eggBuilder = buildSonHaiEggData(eggData, user.getLanguage());
            builder.setEgg(eggBuilder);
            if (plotData.getUserId() != user.getUserId()) {
                builder.setHelpCdEndTime(eggData.getHelpCdEndTime(user.getUserId(), user.getConfig().getHelpCd()));
            }
        }
        //兽王数据
        if (beastBossData != null) {
            SonHaiProto.SonHaiBeastBossData.Builder beastBossBuilder = builderSonHaiBeastBossData(beastBossData);
            builder.setBeastBossData(beastBossBuilder);
        }
        return builder;
    }

    /**
     * build 聊天协助地块信息
     * @param plotData
     * @return
     */
    public static SonHaiProto.SonHaiChatMsgPlotData.Builder buildSonHaiChatMsgPlotData(SonHaiPlotData plotData) {
        SonHaiProto.SonHaiChatMsgPlotData.Builder builder = SonHaiProto.SonHaiChatMsgPlotData.newBuilder();
        builder.setUserId(plotData.getUserId());
        builder.setRegionId(plotData.getRegionId());
        builder.setId(plotData.getPlotId());
        builder.setX(plotData.getX());
        builder.setY(plotData.getY());
        builder.setWinFlag(plotData.getWinFlag());
        builder.setRegionUniqueId(plotData.getRegionUniqueId());
        return builder;
    }

    /**
     * build 门客数据
     * @param patronsData
     * @return
     */
    public static SonHaiProto.SonHaiPatronsData.Builder buildSonHaiPatronsData(SonHaiPatronsData patronsData) {
        SonHaiProto.SonHaiPatronsData.Builder builder = SonHaiProto.SonHaiPatronsData.newBuilder();
        builder.setPatronsId(patronsData.getPatronsId());
        builder.setOccupation(patronsData.getOccupation());
        builder.setBasePower(patronsData.getAbility());
        builder.setDefendAllBlood(patronsData.getAllBlood());
        builder.setReBlood(patronsData.getAllReBlood());
        for (SonHaiReBloodInfo reBloodInfo : patronsData.getReBloodInfoMap().values()) {
            SonHaiProto.SonHaiReBloodInfo.Builder reBloodBuilder = buildSonHaiReBloodInfo(reBloodInfo);
            builder.addReBloodInfos(reBloodBuilder);
        }
        builder.setQuality(patronsData.getQuality());
        builder.setSkinId(patronsData.getSkinId());
        builder.setIsLinkageSwitch(patronsData.isLinkageSwitch());
        return builder;
    }

    /**
     * build 兽蛋数据
     * @param eggData
     * @return
     */
    public static SonHaiProto.SonHaiEggData.Builder buildSonHaiEggData(SonHaiEggData eggData, String language) {
        CrossUserBaseInfo findUser = CrossUserMgr.getCrossUserBaseInfo(eggData.getFindUserId());
        SonHaiProto.SonHaiEggData.Builder builder = SonHaiProto.SonHaiEggData.newBuilder();
        builder.setId(eggData.getId());
        builder.setEggConfigId(eggData.getEggConfigId());
        builder.setFindUserId(eggData.getFindUserId());
        if (findUser != null && findUser.getUserBaseInfo() != null) {
            builder.setFindUserName(findUser.getUserBaseInfo().getNickName());
        }
        SonHaiProto.SonHaiBeastGroupData.Builder beastGroupBuilder = buildSonHaiBeastGroupData(eggData.getBeastGroupData());
        builder.setBeastGroup(beastGroupBuilder);
        //构建PVP玩家
        if (eggData.getUserId() > 0) {
            CrossSonHaiUser user = CrossSonHaiActivityMgr.getUser(eggData.getActivityId(), eggData.getGroupId(), eggData.getUserId());
            if (user != null) {
                SonHaiProto.SonHaiPvPUserData.Builder pvpUser = buildSonHaiPvPUserData(user, eggData.getPatronsDataMap());
                builder.setPvpUser(pvpUser);
            }
        }
        builder.setCompleteTime(eggData.getCompleteTime());
        builder.setEggName(eggData.getEggName() == null ? "" : ServerLanguageMgr.getContent(eggData.getEggName(), language));
        builder.setOccupyTime(eggData.getOccupyTime());
        return builder;
    }

    /**
     * build 蛋奖励数据
     * @param rewardData
     * @return
     */
    public static SonHaiProto.SonHaiEggRewardData.Builder buildSonHaiEggRewardData(SonHaiUserEggRewardData rewardData, SonHaiEggData eggData, String language) {
        SonHaiProto.SonHaiEggRewardData.Builder builder = SonHaiProto.SonHaiEggRewardData.newBuilder();
        builder.setId(rewardData.getEggId());
        builder.setEggConfigId(eggData.getEggConfigId());
        builder.setEggRewardId(rewardData.getId());
        builder.setReward(rewardData.getReward());
        builder.setEggName(ServerLanguageMgr.getContent(eggData.getEggName(), language));
        return builder;
    }

    /**
     * build 创建地块结果
     * @param resp
     * @return
     */
    public static SonHaiProto.SonHaiExploreResult.Builder buildSonHaiExploreResult(SonHaiCreatePlotResp resp) {
        SonHaiProto.SonHaiExploreResult.Builder builder = SonHaiProto.SonHaiExploreResult.newBuilder();
        SonHaiProto.SonHaiPlotData.Builder plotBuilder = buildSonHaiSimplePlotData(resp.getPlotData());
        builder.setPlotData(plotBuilder);
        builder.setOriginPlotLevel(resp.getOriginPlotLevel());
        //直接镇伏的结果数据
        if (resp.getWinPlotResp() != null) {
            builder.setReward(resp.getWinPlotResp().getReward());
            builder.setAddProgress(resp.getWinPlotResp().getAddProgress());
            builder.setAddScore(resp.getWinPlotResp().getAddScore());
            builder.setAddEnergy(resp.getWinPlotResp().getAddEnergy());
            builder.setAddBloodRate(resp.getWinPlotResp().getAddBloodRate());
        }
        //额外产生的地块
        if (resp.getExtraPlotDataList() != null && !resp.getExtraPlotDataList().isEmpty()) {
            for (SonHaiCreatePlotResp plotResp : resp.getExtraPlotDataList()) {
                SonHaiProto.SonHaiExploreResult.Builder extraPlotBuilder = buildSonHaiExploreResult(plotResp);
                builder.addExtraPlotDataList(extraPlotBuilder);
            }
        }
        return builder;
    }

    /**
     * build 玩家PVP数据
     * @param user
     * @return
     */
    public static SonHaiProto.SonHaiPvPUserData.Builder buildSonHaiPvPUserData(CrossSonHaiUser user, Map<Integer, SonHaiPatronsData> patronsDataMap) {
        SonHaiProto.SonHaiPvPUserData.Builder builder = SonHaiProto.SonHaiPvPUserData.newBuilder();
        builder.setUserId(user.getUserId());
        CrossUserBaseInfo userBase = CrossUserMgr.getCrossUserBaseInfo(user.getUserId());
        if (userBase != null && userBase.getUserBaseInfo() != null) {
            builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBase.getUserBaseInfo()));
        }
        builder.setPvpKillNum(user.getPvpKillNum());
        builder.setAllBlood(user.getAllBlood());
        builder.setReBlood(user.getReBlood());
        for (SonHaiReBloodInfo reBloodInfo : user.getReBloodInfoMap().values()) {
            builder.addReBloodInfos(buildSonHaiReBloodInfo(reBloodInfo));
        }
        if (patronsDataMap != null) {
            for (SonHaiPatronsData patronsData : patronsDataMap.values()) {
                builder.addPatronsData(buildSonHaiPatronsData(patronsData));
            }
        }
        return builder;
    }

    /**
     * 获取协助玩家数据
     * @param user
     * @param regionData
     * @return
     */
    public static SonHaiProto.SonHaiHelpUserData.Builder buildSonHaiHelpUserData(CrossSonHaiUser user, SonHaiRegionData regionData) {
        SonHaiProto.SonHaiHelpUserData.Builder builder = SonHaiProto.SonHaiHelpUserData.newBuilder();
        builder.setUserId(user.getUserId());
        CrossUserBaseInfo userBase = CrossUserMgr.getCrossUserBaseInfo(user.getUserId());
        if (userBase != null && userBase.getUserBaseInfo() != null) {
            builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBase.getUserBaseInfo()));
        }
        builder.setPvePower(user.getPowerForPve(regionData));
        builder.setPvpPower(user.getPowerForPvp());
        return builder;
    }

    /**
     * 地块buff
     * @param resp
     * @return
     */
    public static SonHaiProto.SonHaiPlotBuffData.Builder buildSonHaiPlotBuffData(SonHaiPlotBuffResp resp) {
        SonHaiProto.SonHaiPlotBuffData.Builder builder = SonHaiProto.SonHaiPlotBuffData.newBuilder();
        builder.setPlotId(resp.getPlotEventId());
        builder.setX(resp.getX());
        builder.setY(resp.getY());
        builder.setValue(resp.getValue());
        return builder;
    }

    /**
     * build one by one 战斗结果
     * @param battleOneResp
     * @return
     */
    public static SonHaiProto.SonHaiBattleResult.Builder builderSonHaiBattleResult(SonHaiBattleOneResp battleOneResp) {
        SonHaiProto.SonHaiBattleResult.Builder builder = SonHaiProto.SonHaiBattleResult.newBuilder();
        builder.setWin(battleOneResp.isWinFlag());
        builder.setDamage(battleOneResp.getDamage());
        builder.setMyPower(battleOneResp.getMyPower());
        builder.setTargetPower(battleOneResp.getTargetPower());
        return builder;
    }

    /**
     * 构建新版战斗结果
     * @param newBattleResp
     * @return
     */
    public static SonHaiProto.SonHaiNewBattleResult.Builder builderSonHaiNewBattleResult(SonHaiNewBattleResp newBattleResp) {
        SonHaiProto.SonHaiNewBattleResult.Builder builder = SonHaiProto.SonHaiNewBattleResult.newBuilder();
        builder.setWin(newBattleResp.isWinFlag());
        builder.setMyBattleResult(builderSonHaiBattleResult(newBattleResp.getBattleOneResp()));
        builder.setMemberId(newBattleResp.getMemberId());
        if (newBattleResp.getMemberBattleOneResp() != null) {
            builder.setMemberBattleResult(builderSonHaiBattleResult(newBattleResp.getMemberBattleOneResp()));
        }
        builder.setReward(newBattleResp.getReward());
        builder.setAddProgress(newBattleResp.getAddProgress());
        builder.setAddScore(newBattleResp.getAddScore());
        builder.setAddAuraScore(newBattleResp.getAddAuraScore());
        builder.setRecoverHp(newBattleResp.getRecoverHp());
        //加成
        builder.setScoreAddRate(newBattleResp.getScoreAddRate());
        builder.setScoreAddValue(newBattleResp.getScoreAddValue());
        builder.setProgressAddRate(newBattleResp.getProgressAddRate());
        builder.setProgressAddValue(newBattleResp.getProgressAddValue());
        builder.setAuraScoreAddRate(newBattleResp.getAuraScoreAddRate());
        builder.setAuraScoreAddValue(newBattleResp.getAuraScoreAddValue());
        return builder;
    }

    /**
     * build 战斗日志
     * @param log
     * @return
     */
    public static SonHaiProto.SonHaiBattleLog.Builder buildSonHaiBattleLog(SonHaiBattleLog log, String language) {
        SonHaiProto.SonHaiBattleLog.Builder logBuilder = SonHaiProto.SonHaiBattleLog.newBuilder();
        logBuilder.setType(log.getType());
        logBuilder.setEventId(log.getEventId());
        logBuilder.setUserId(log.getUserId());
        logBuilder.setTargetUserId(log.getTargetUserId());
        CrossUserBaseInfo userBase = CrossUserMgr.getCrossUserBaseInfo(log.getUserId());
        if (userBase != null && userBase.getUserBaseInfo() != null) {
            logBuilder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBase.getUserBaseInfo()));
        }
        CrossUserBaseInfo targetUserBase = CrossUserMgr.getCrossUserBaseInfo(log.getTargetUserId());
        if (targetUserBase != null && targetUserBase.getUserBaseInfo() != null) {
            logBuilder.setTargetUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(targetUserBase.getUserBaseInfo()));
        }
        logBuilder.setEggId(log.getEggId());
        logBuilder.setEggConfigId(log.getEggConfigId());
        logBuilder.setEggName(log.getEggName() == null ? "" : ServerLanguageMgr.getContent(log.getEggName(), language));
        logBuilder.setReward(log.getReward() == null ? "" : log.getReward());
        logBuilder.setLoseAuraScore(log.getLoseAuraScore());
        logBuilder.setTime(log.getCreateTime());
        logBuilder.setAddScore(log.getScore());
        logBuilder.setAddAuraScore(log.getAuraScore());
        return logBuilder;
    }

    /**
     * 死亡相关信息
     * @param dieData
     * @return
     */
    public static SonHaiProto.SonHaiUserDieData.Builder buildSonHaiUserDieData(SonHaiUserDieData dieData) {
        SonHaiProto.SonHaiUserDieData.Builder builder = SonHaiProto.SonHaiUserDieData.newBuilder();
        if (dieData == null) {
            return builder;
        }
        CrossUserBaseInfo userBase = CrossUserMgr.getCrossUserBaseInfo(dieData.getTargetUserId());
        if (userBase != null && userBase.getUserBaseInfo() != null) {
            builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBase.getUserBaseInfo()));
        }
        builder.setLoseAuraScore(dieData.getReAureScore());
        return builder;
    }

    /******************************************解析******************************************/

    /**
     * 解析 parsePatronsMap
     * @param list
     * @return
     */
    public static Map<Integer, SonHaiPatronsData> parsePatronsMap(List<SonHaiProto.SonHaiPatronsData> list) {
        Map<Integer, SonHaiPatronsData> patronsMap = new LinkedHashMap<>();
        for (SonHaiProto.SonHaiPatronsData patronsPro : list) {
            SonHaiPatronsData patrons = new SonHaiPatronsData();
            patrons.setPatronsId(patronsPro.getPatronsId());
            patrons.setOccupation(patronsPro.getOccupation());
            patrons.setAbility(patronsPro.getBasePower());
            patrons.setQuality(patronsPro.getQuality());
            patrons.setSkinId(patronsPro.getSkinId());
            patrons.setLinkageSwitch(patronsPro.getIsLinkageSwitch());
            patronsMap.put(patrons.getPatronsId(), patrons);
        }
        return patronsMap;
    }
}
